package br.com.algarmidia.models;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import br.com.caelum.vraptor.validator.ValidationMessage;

//A anotação @javax.persistence.Entity mostra que a classe é uma entidade de persistência.
//As entidades devem ser mapeadas no arquivo de configuração do hibernate
@javax.persistence.Entity
public class User extends Entity {

	private String name;
	private String password;
	private String email;

	public User() {}

	public User(Long id, String name, String password, String email) {
		super();
		setId(id);
		this.name = name;
		this.password = password;
		this.email = email;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	public String getEmail() {
		return email;
	}

	public List<ValidationMessage> validate() {
		List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
		if (StringUtils.isEmpty(getName())) {
			messages.add(new ValidationMessage("user.name.empty", "error"));
		} if (StringUtils.isEmpty(getPassword())) {
			messages.add(new ValidationMessage("user.password.empty", "error"));
		} if (StringUtils.isEmpty(getEmail())) {
			messages.add(new ValidationMessage("user.email.empty", "error"));
		}
		return messages;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (getId() == null) {
			if (other.getId() != null)
				return false;
		} else if (!getId().equals(other.getId()))
			return false;
		return true;
	}

}
